// pages/screen/index.js
const api = require('../../utils/api')
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activeKey:0,
        checked1:0,
        tabList:[],
        idList:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
        // this.setData({
        //   idList: wx.getStorageSync("idList") 
        // })

        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    restart(){
        let that = this
        for(let i=0;i<that.data.tabList.length;i++){
          that.data.tabList[i].child.find((item)=>{
            item.checked=false
          })
        }
        let arr =  that.handleTableList(that.data.tabList)
        that.setData({
            tabList:arr
        })
        that.setData({
            idList:[]
        })
        wx.setStorageSync('idList', that.data.idList)
    },
    handleSub(){
        app.globalData.isSearch = true
        wx.navigateBack({
          delta: 0,
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      wx.setStorageSync('idList', [])
        api.getCategoryList({
            data: {
                opcode: "1",
                pagesize: 10,
                page: 1,
              }
            ,
            success:(res)=>{
                console.log(res,'aaa')
                let data = res.data.data.data
                var category = {
                    1001: "车型",
                    1002: "品牌",
                    1003: "价格",
                    1004: "公里数",
                    1005: "车龄",
                  };
                  let dataList = new Array();
                  let child = new Array();
                  let dataDict = {};
                  for (let k in data) {
                    dataDict["id"] = Number(k);
                    dataDict["name"] = category[k];
                    for (let j in data[k]) {
                      data[k][j] = JSON.parse(data[k][j]);
                      data[k][j]["id"] = Number(j);
                      child.push(data[k][j]);
                    }
                    dataDict["child"] = child;
                    dataList.push(dataDict);
                    dataDict = {};
                    child = [];
                  }
                  let arr =  this.handleTableList(dataList)
                  this.setData({
                    tabList:arr
                  })

            }
        })
    },
    handleCheck(e){
      console.log(e,'aee')
      let that = this
      let id = e.currentTarget.id
      for(let i=0;i<that.data.tabList.length;i++){
        that.data.tabList[i].child.find((item)=>{
          if(item.id==id){
              item.checked=!item.checked
              if(!wx.getStorageSync('idList').includes(+id)){
                that.data.idList.push(+id)
              }else{
                let index = that.data.idList.indexOf(+id)
                that.data.idList.splice(index,1)
              }
            that.setData({
              tabList:that.data.tabList,
              idList:that.data.idList
            })
            wx.setStorageSync('idList', that.data.idList)
          }else{
            
          }
        })
      }
    },
    handleTableList(list){
      return list.map((item)=>{
        if(item.child.length>0){
          let child = item.child.map((i)=>{
            return {
              ...i,
              checked:false
            }
          })
          return {
            name:item.name,
            id:item.id,
            child
          }
        }else{
          return {
            ...item
          }
        }
      })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})